package cn.tedu.tmall.admin.content.service.impl;

import cn.tedu.tmall.admin.content.dao.repository.IArticleRepository;
import cn.tedu.tmall.admin.content.dao.repository.ICategoryRepository;
import cn.tedu.tmall.admin.content.pojo.param.ArticleAddParam;
import cn.tedu.tmall.admin.content.pojo.po.ArticleDetailPO;
import cn.tedu.tmall.admin.content.pojo.po.ArticlePO;
import cn.tedu.tmall.admin.content.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.content.pojo.vo.ArticleDetailVO;
import cn.tedu.tmall.admin.content.pojo.vo.ArticleListVO;
import cn.tedu.tmall.admin.content.service.IArticleService;
import cn.tedu.tmall.common.pojo.PageData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ArticleService implements IArticleService {
    /**
     * //验证登录状态
     * //1 文章标题不能重复
     * //2 类别是否存在
     * //3 类别如果被禁用了,是否可以在当前类别下发文章?
     * //    可以发,但是类别不显示,导致这个文章也不显示
     * //    我们觉得 如果类别被禁用,不让发 报错 当前类别已被禁用
     * //4 保存文章,如果成功,返回,如果保存失败,异常
     */
    @Autowired
    private ICategoryRepository categoryRepository;
    @Autowired
    private IArticleRepository articleRepository;

    @Override
    public void addArticle(ArticleAddParam articleAddParam) {


        //1 判断文章标题是否重复
        String title = articleAddParam.getTitle();
        Integer articleCount = articleRepository.countArticleByTitle(title);
        if (articleCount == 0) {
            //不重复
            Long categoryId = articleAddParam.getCategoryId();
            //获取类别信息
            //当前方法 getCategoryById 返回值是什么
            // 1 数字  2 具体的类别
            CategoryPO categoryPO = categoryRepository.getCategoryById(categoryId);
            if (categoryPO == null) {
                throw new RuntimeException("类别不存在,文章发送失败!");
            } else {
                Integer enable = categoryPO.getEnable();
                if (enable == 1) {  //类别状态为启用状态
                    ArticlePO articlePO = new ArticlePO();
                    BeanUtils.copyProperties(articleAddParam, articlePO);
                    Integer articleRows = articleRepository.saveArticle(articlePO);
                    if (articleRows != 1) {
                        throw new RuntimeException("文章发送失败!");
                    }
                    ArticleDetailPO articleDetailPO = new ArticleDetailPO();
                    articleDetailPO.setArticleId(articlePO.getId());//articleId
                    articleDetailPO.setDetail(articleAddParam.getContent());
                    //这行代码出问题了. saveArticleDetail 这个操作失败了,
                    // 文章的基本信息保存成功了,文章详细保存失败.
                    // 模拟一个异常 处理这种情况 把数据进行回滚
                    // 滚谁 把Article 基本信息数据删掉 ,恢复到原来的状态 saveArticle代码执行前的状态
                    "".substring(10);
                    Integer articleDetailRows = articleRepository.saveArticleDetail(articleDetailPO);
                    if (articleDetailRows != 1) {
                        throw new RuntimeException("文章发送失败!");
                    }
                } else {
                    throw new RuntimeException("类别已经被禁用,文章发送失败!");
                }
            }
        } else {
            throw new RuntimeException("文章标题已经存在,文章发送失败!");
        }
    }

    @Override
    public PageData<ArticleListVO> listArticle(Long categoryId, Integer pageNum, Integer pageSize) {
        //默认值 可选 如果用没传这个两个参数使用默认值,
        //pageSize 需要加验证 不能超过20
        PageData<ArticlePO> articlePOS = articleRepository.listArticle(categoryId,pageNum,pageSize);
        List<ArticleListVO> articleItemListVOS = articleListPO2ListVO(articlePOS.getList());
        PageData<ArticleListVO> result = new PageData<>();
        BeanUtils.copyProperties(articlePOS,result);
        result.setList(articleItemListVOS);
        return result;
    }

    private ArticleListVO articlePO2VO(ArticlePO po) {
        ArticleListVO vo = new ArticleListVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    private List<ArticleListVO> articleListPO2ListVO(List<ArticlePO> pos) {
        List<ArticleListVO> vos = new ArrayList<>();
        for (ArticlePO po : pos) {
            vos.add(articlePO2VO(po));
        }
        return vos;
    }


    @Override

    public ArticleDetailVO detailArticle(Long articleId) {
        ArticlePO articlePO = articleRepository.basic(articleId);
        if (articlePO == null) {
            throw new RuntimeException("文章不存在");
        }
        ArticleDetailPO articleDetailPO = articleRepository.detail(articleId);
        if (articleDetailPO == null) {
            throw new RuntimeException("文章不存在");
        }
        ArticleDetailVO articleDetailVO = new ArticleDetailVO();
        BeanUtils.copyProperties(articleDetailPO, articleDetailVO);
        BeanUtils.copyProperties(articlePO, articleDetailVO);
        return articleDetailVO;
    }
}
